<template>
  <div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" @close="closeDialog" :destroy-on-close="true" :append-to-body="true" :close-on-click-modal="false">
      <el-form :model="roleForm" :rules="roleRules" ref="roleForm">
        <el-form-item label="角色名称" :label-width="formLabelWidth" prop="roleName">
          <el-input v-model="roleForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色key" :label-width="formLabelWidth" prop="roleKey">
          <el-input v-model="roleForm.roleKey" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="roleForm.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth" prop="orderNum">
          <el-input v-model="roleForm.orderNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色状态" :label-width="formLabelWidth" prop="status">
          <el-radio-group v-model="roleForm.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addOrEditMenu">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addRole, editRole, getRoles} from "@/api/role";
import {Message} from "element-ui";

export default {
  name: "RoleDialog",
  data() {
    return {
      formLabelWidth: '120px',
      roleRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        roleKey: [
          { required: true, message: '请输入角色key', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择角色状态', trigger: 'change' }
        ],
      }
    }
  },
  computed: {
    dialogFormVisible: {
      get() {
        return this.$store.state.role.dialogFormVisible;
      },
      set(value) {
        this.$store.state.role.dialogFormVisible = value;
      }
    },
    dialogTitle() {
      return this.$store.state.role.dialogTitle;
    },
    roleForm: {
      get() {
        return this.$store.state.role.roleForm;
      },
      set(value) {
        this.$store.state.role.roleForm = value;
      }
    }
  },
  methods: {
    refreshData() {
      var params = {
        "current": this.$store.state.role.currentPage,
        "size": this.$store.state.role.pageSize
      };
      // 刷新
      getRoles(params).then((res) => {
        this.$store.state.role.tableData = res.data.data;
        this.$store.state.role.total = res.data.total;
        this.refreshData();
      })
    },
    clearForm() {
      Object.keys(this.roleForm).forEach((key) => {
        this.roleForm[key] = null;
      })
    },
    closeDialog() {
      this.$store.state.role.dialogFormVisible = false;
      this.clearForm();
    },
    addOrEditMenu() {
      this.$refs["roleForm"].validate((valid) => {
        if (valid) {
          if (this.dialogTitle === "添加角色") {
            addRole(this.roleForm).then((res) => {
              Message.success(res.data.msg);
              this.dialogFormVisible = false;
              this.refreshData();
            })
          } else {
            editRole(this.roleForm).then((res) => {
              Message.success(res.data.msg);
              this.dialogFormVisible = false;
              this.refreshData();
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>

</style>
